package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverHotShareActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRankActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRecomentAlbumActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRecommendActiActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverSpecialActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverWebActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.ListenerActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRecommendFootVPadapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_Recommend_ListviewAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_Recommend_ViewpagerAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRecommendFootVPService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRecommendHotRecommendService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRecommendSpecialService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRecommendDiscoverColumns;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRecommendFootView;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRecommendHotRecommend;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_RecommendBanner;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_Recommend_FocusImageBean;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.qiaoyanqing.ximalaya.util.MyLog;
import com.qiaoyanqing.ximalaya.util.MyOkHttpUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRecommendFragment extends Fragment implements Handler.Callback, ViewPager.OnPageChangeListener, Callback<DiscoverRecommendHotRecommend> {

    // 头布局轮播
    private List<ImageView> mData;
    private ListView mListView;
    private ViewPager mViewPager;
    private LinearLayout mTabscontainer;
    private Discover_Recommend_ViewpagerAdapter mVPadapter;
    private List<DiscoverRecommendDiscoverColumns.EditorRecommendAlbumsBean.ListBean> mList = new ArrayList<>();
    private List<DiscoverRecommendHotRecommend.HotRecommendsBean.ListBean> mListBeanData;
    private List<Discover_Recommend_FocusImageBean> mBannerList = new ArrayList<>();
    private Discover_Recommend_ListviewAdapter mLVadapter;
    private Handler mHandler = new Handler((Handler.Callback) this);
    //listview集合
    private List<DiscoverRecommendHotRecommend.GuessBean.ListBean> mGuessList=new ArrayList<>();
    private List<DiscoverRecommendDiscoverColumns.DiscoveryColumnsBean.ListBean>mDiscoveryColumnsBean=new ArrayList<>();
    private List<DiscoverRecommendDiscoverColumns.SpecialColumnBean.ListBean>mSpecialBean=new ArrayList<>();

    //精品听单
    private LinearLayout specialListener;
    private ImageView Specialimg1;
    private TextView SpecialTitle1, SpecialSubtitle1, Specialnum1;
    //小编推荐
    private TextView EditTitle1, Editsubtitle1, Editintitle1, EditTitle2, Editsubtitle2, Editintitle2, EditTitle3, Editsubtitle3, Editintitle3;
    private ImageView EditImg1, EditImg2, EditImg3;
    //猜你喜欢
    private ImageView Guessimg1,Guessimg2,Guessimg3,Guessimg4,Guessimg5,Guessimg6;
    private TextView GuessTitle1,GuessTitle2,GuessTitle3,GuessTitle4,GuessTitle5,GuessTitle6;
    private TextView Guesssubtitle1,Guesssubtitle2,Guesssubtitle3,Guesssubtitle4,Guesssubtitle5,Guesssubtitle6;
    //发现新奇
    private ImageView Img1,Img2,Img3,Img4,Img5,Img6,Img7,Img8;
    private TextView Sctext1,Sctext2,Sctext3,Sctext4,Sctext5,Sctext6,Sctext7,Sctext8;
    //尾布局
    private ViewPager FootViewpager;
    private List<ImageView>mFootImage;
    private DiscoverRecommendFootVPadapter mFootVPadapter;
    private LinearLayout FootIndicator;
    // 监听
    private RelativeLayout mEditrecommend;



    public DiscoverRecommendFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        mListView = (ListView) view.findViewById(R.id.reconmmend_listview);


        //加载数据，
        initData();

        initListview();
        //点击事件
        initListener();

        return view;
    }
    //TODO
    private void initListener() {
        mViewPager.setCurrentItem(currentPosition);
        mViewPager.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //   view.


            }
        });





        //发现新奇,听咨询
        Img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath","http://fdfs.xmcdn.com/group10/M07/CB/31/wKgDaVZxIG7y6MciAADXlSjVU68904_mobile_small.jpg");
                intent.putExtra("nickname", "头条滚出来");
                intent.putExtra("title", "头条滚出来");
                intent.putExtra("album", mDiscoveryColumnsBean.get(0).getProperties().getAlbumId());
                intent.putExtra("playcounts","21510000");
                startActivity(intent);

            }
        });
        //今日最火
        Img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent=new Intent(getContext(),DiscoverRankActivity.class);
               intent.putExtra("key",mDiscoveryColumnsBean.get(1).getProperties().getKey());
                MyLog.i("flag","------key"+mDiscoveryColumnsBean.get(1).getProperties().getKey().toString());
                intent.putExtra("title",mDiscoveryColumnsBean.get(1).getTitle());
                intent.putExtra("contentType",mDiscoveryColumnsBean.get(1).getContentType());


            //    startActivity(intent);

            }
        });



        //商城Webview
        Img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),DiscoverWebActivity.class);
                intent.putExtra("url",mDiscoveryColumnsBean.get(3).getUrl());

                startActivity(intent);
            }
        });
        //游戏中心Webview
        Img5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),DiscoverWebActivity.class);

                intent.putExtra("url","http://game.ximalaya.com/games-operation/v1/games/list");

                startActivity(intent);
            }
        });

       //热门分享
        Img6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),DiscoverHotShareActivity.class);

             //   intent.putExtra("key",mDiscoveryColumnsBean.get(5).getProperties().getKey());

                startActivity(intent);

            }
        });

        //活动
        Img7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(),DiscoverRecommendActiActivity.class);
                //  intent.putExtra("url",mDiscoveryColumnsBean.get(4).getUrl());
                intent.putExtra("url","http://activity.ximalaya.com/activity-web/activity/activityList?device=android&pageId=1");
                startActivity(intent);
            }
        });

        Img8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             Intent intent=new Intent(getContext(),ListenerActivity.class);

                startActivity(intent);

            }
        });




        //精品听单
        specialListener.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getContext(), DiscoverSpecialActivity.class);

                intent.putExtra("id",mSpecialBean.get(0).getSpecialId());
                MyLog.i("QQQQQQQ",mSpecialBean.get(0).getSpecialId()+"");

                startActivity(intent);
            }
        });
        //猜你喜欢
        Guessimg1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath",mGuessList.get(0).getCoverSmall());
                intent.putExtra("nickname", mGuessList.get(0).getNickname());
                intent.putExtra("title", mGuessList.get(0).getTitle());
                intent.putExtra("album", mGuessList.get(0).getAlbumId());
                intent.putExtra("tags", mGuessList.get(0).getTags());
                intent.putExtra("playcounts",mGuessList.get(0).getPlaysCounts());
                startActivity(intent);

            }
        });

        Guessimg2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath",mGuessList.get(1).getCoverSmall());
                intent.putExtra("nickname", mGuessList.get(1).getNickname());
                intent.putExtra("title", mGuessList.get(1).getTitle());
                intent.putExtra("album", mGuessList.get(1).getAlbumId());
                intent.putExtra("tags", mGuessList.get(1).getTags());
                intent.putExtra("playcounts",mGuessList.get(1).getPlaysCounts());
                startActivity(intent);

            }
        });
        Guessimg3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath",mGuessList.get(2).getCoverSmall());
                intent.putExtra("nickname", mGuessList.get(2).getNickname());
                intent.putExtra("title", mGuessList.get(2).getTitle());
                intent.putExtra("album", mGuessList.get(2).getAlbumId());
                intent.putExtra("tags", mGuessList.get(2).getTags());
                intent.putExtra("playcounts",mGuessList.get(2).getPlaysCounts());
                startActivity(intent);

            }
        });
        Guessimg4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath",mGuessList.get(3).getCoverSmall());
                intent.putExtra("nickname", mGuessList.get(3).getNickname());
                intent.putExtra("title", mGuessList.get(3).getTitle());
                intent.putExtra("album", mGuessList.get(3).getAlbumId());
                intent.putExtra("tags", mGuessList.get(3).getTags());
                intent.putExtra("playcounts",mGuessList.get(3).getPlaysCounts());
                startActivity(intent);

            }
        });
        Guessimg5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath",mGuessList.get(4).getCoverSmall());
                intent.putExtra("nickname", mGuessList.get(4).getNickname());
                intent.putExtra("title", mGuessList.get(4).getTitle());
                intent.putExtra("album", mGuessList.get(4).getAlbumId());
                intent.putExtra("tags", mGuessList.get(4).getTags());
                intent.putExtra("playcounts",mGuessList.get(4).getPlaysCounts());
                startActivity(intent);

            }
        });
        Guessimg6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath",mGuessList.get(5).getCoverSmall());
                intent.putExtra("nickname", mGuessList.get(5).getNickname());
                intent.putExtra("title", mGuessList.get(5).getTitle());
                intent.putExtra("album", mGuessList.get(5).getAlbumId());
                intent.putExtra("tags", mGuessList.get(5).getTags());
                intent.putExtra("playcounts",mGuessList.get(5).getPlaysCounts());
                startActivity(intent);

            }
        });

        //小编推荐
        EditImg1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath", mList.get(0).getCoverMiddle());
                intent.putExtra("nickname", mList.get(0).getNickname());
                intent.putExtra("title", mList.get(0).getTitle());
                intent.putExtra("album", mList.get(0).getAlbumId());
                intent.putExtra("tags", mList.get(0).getTags());
                intent.putExtra("playcounts", mList.get(0).getPlaysCounts());
                startActivity(intent);
            }
        });
        EditImg2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath", mList.get(1).getCoverMiddle());
                intent.putExtra("nickname", mList.get(1).getNickname());
                intent.putExtra("title", mList.get(1).getTitle());
                intent.putExtra("album", mList.get(1).getAlbumId());
                intent.putExtra("tags", mList.get(1).getTags());
                intent.putExtra("playcounts", mList.get(1).getPlaysCounts());
                startActivity(intent);

            }
        });
        EditImg3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), DiscoverRecomentAlbumActivity.class);
                intent.putExtra("iconpath", mList.get(2).getCoverMiddle());
                intent.putExtra("nickname", mList.get(2).getNickname());
                intent.putExtra("title", mList.get(2).getTitle());
                intent.putExtra("album", mList.get(2).getAlbumId());
                intent.putExtra("tags", mList.get(2).getTags());
                intent.putExtra("playcounts", mList.get(2).getPlaysCounts());
                startActivity(intent);

            }
        });


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });



    }




    // 记录viewpager当前的位置
    private int currentPosition = Integer.MAX_VALUE / 2;

    private void initListview() {

        //mDiscoverRecommendDiscoverColumnsData =new ArrayList<>();
        mListBeanData = new ArrayList<>();

        mLVadapter = new Discover_Recommend_ListviewAdapter(mListBeanData, getContext());


        //TODO
        //添加头布局是，里面有一个viewpager，还有一个盛庄小球的容器
        View headview = LayoutInflater.from(getContext()).inflate(R.layout.recommendhead_view, mListView, false);
        //设置viewpager 还有适配器
        mViewPager = (ViewPager) headview.findViewById(R.id.viewpager);

        //添加尾布局
        View footview= LayoutInflater.from(getContext()).inflate(R.layout.recommend_footview,mListView,false);
        FootViewpager= (ViewPager) footview.findViewById(R.id.recommendfootviewpager);
        FootIndicator= (LinearLayout) footview.findViewById(R.id.footindicator);

        /**精品听单*/
        initSpecial(headview);
        /**小编推荐*/
        initEditRecimmend(headview);
        /**猜你喜欢*/
        initGuess(headview);
        /**发现新奇*/
        initDiscoveryColumns(headview);


        //实例化mData
        mData = new ArrayList<>();

        mVPadapter = new Discover_Recommend_ViewpagerAdapter(mData);

        mViewPager.setAdapter(mVPadapter);
        mViewPager.setCurrentItem(currentPosition);
        mViewPager.addOnPageChangeListener(this);
        //设置小球
        mTabscontainer = (LinearLayout) headview.findViewById(R.id.tabscontainer);

        mListView.addHeaderView(headview);
         //  mListView.addFooterView(footview);
        //设置lisview的适配器

        mListView.setAdapter(mLVadapter);

    }

    private void initDiscoveryColumns(View headview) {
        Img1= (ImageView) headview.findViewById(R.id.image1);
        Img2= (ImageView) headview.findViewById(R.id.image2);
        Img3= (ImageView) headview.findViewById(R.id.image3);
        Img4= (ImageView) headview.findViewById(R.id.image4);
        Img5= (ImageView) headview.findViewById(R.id.image5);
        Img6= (ImageView) headview.findViewById(R.id.image6);
        Img7= (ImageView) headview.findViewById(R.id.image7);
        Img8= (ImageView) headview.findViewById(R.id.image8);
        Sctext1= (TextView) headview.findViewById(R.id.Sctext1);
        Sctext2= (TextView) headview.findViewById(R.id.Sctext2);
        Sctext3= (TextView) headview.findViewById(R.id.Sctext3);
        Sctext4= (TextView) headview.findViewById(R.id.Sctext4);
        Sctext5= (TextView) headview.findViewById(R.id.Sctext5);
        Sctext6= (TextView) headview.findViewById(R.id.Sctext6);
        Sctext7= (TextView) headview.findViewById(R.id.Sctext7);
        Sctext8= (TextView) headview.findViewById(R.id.Sctext8);


    }

    private void initGuess(View headview) {
        Guessimg1= (ImageView) headview.findViewById(R.id.guessimg1);
        Guessimg2= (ImageView) headview.findViewById(R.id.guessimg2);
        Guessimg3= (ImageView) headview.findViewById(R.id.guessimg3);
        Guessimg4= (ImageView) headview.findViewById(R.id.guessimg4);
        Guessimg5= (ImageView) headview.findViewById(R.id.guessimg5);
        Guessimg6= (ImageView) headview.findViewById(R.id.guessimg6);
        GuessTitle1= (TextView) headview.findViewById(R.id.guesstitle1);
        GuessTitle2= (TextView) headview.findViewById(R.id.guesstitle2);
        GuessTitle3= (TextView) headview.findViewById(R.id.guesstitle3);
        GuessTitle4= (TextView) headview.findViewById(R.id.guesstitle4);
        GuessTitle5= (TextView) headview.findViewById(R.id.guesstitle5);
        GuessTitle6= (TextView) headview.findViewById(R.id.guesstitle6);
        Guesssubtitle1= (TextView) headview.findViewById(R.id.guesssubtitle1);
        Guesssubtitle2= (TextView) headview.findViewById(R.id.guesssubtitle2);
        Guesssubtitle3= (TextView) headview.findViewById(R.id.guesssubtitle3);
        Guesssubtitle4= (TextView) headview.findViewById(R.id.guesssubtitle4);
        Guesssubtitle5= (TextView) headview.findViewById(R.id.guesssubtitle5);
        Guesssubtitle6= (TextView) headview.findViewById(R.id.guesssubtitle6);

    }

    private void initEditRecimmend(View headview) {

        mEditrecommend= (RelativeLayout) headview.findViewById(R.id.editrecommend);
        //图片下面的第一句话
        EditTitle1 = (TextView) headview.findViewById(R.id.first_text);
        EditTitle2 = (TextView) headview.findViewById(R.id.mid_text);
        EditTitle3 = (TextView) headview.findViewById(R.id.third_text);
        EditImg1 = (ImageView) headview.findViewById(R.id.first_image);
        EditImg2 = (ImageView) headview.findViewById(R.id.mid_image);
        EditImg3 = (ImageView) headview.findViewById(R.id.third_image);
        //图片下面的最后一句话
        Editsubtitle1 = (TextView) headview.findViewById(R.id.inTitle_first);
        Editsubtitle2 = (TextView) headview.findViewById(R.id.inTitle_mid);
        Editsubtitle3 = (TextView) headview.findViewById(R.id.inTitle_third);

    }

    private void initSpecial(View headview) {
        specialListener= (LinearLayout) headview.findViewById(R.id.specialListener);
        Specialimg1 = (ImageView) headview.findViewById(R.id.speclialimg1);
        SpecialTitle1 = (TextView) headview.findViewById(R.id.specialtitle1);
        SpecialSubtitle1 = (TextView) headview.findViewById(R.id.specialsubtitle1);
        Specialnum1 = (TextView) headview.findViewById(R.id.specialsonnumber1);


    }

    private void initData() {
        //头布局轮播，改成retrofit就会出现小球数量翻倍。
        MyOkHttpUtils.getNetData(Urlconfig.RECOMMEND_HEADURL, mHandler);

        //精品听单 ,小编推荐，发现新奇
        initSpecialData();
        //热门推荐，猜你喜欢,尾布局
        initHotRecommendRetrofit();
        //尾布局
        //  initFootDataRetrfit();


    }

    private void initFootDataRetrfit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.RECOMMEND_BOTTOM)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRecommendFootVPService service=retrofit.create(DiscoverRecommendFootVPService.class);
        service.getfoot().enqueue(new Callback<DiscoverRecommendFootView>() {
            @Override
            public void onResponse(Call<DiscoverRecommendFootView> call, Response<DiscoverRecommendFootView> response) {
                mFootImage=new ArrayList<ImageView>();
              //  for (int i = 0; i < response.body().getData().get(0).getCover().length(); i++) {
                    for (int i = 0; i < 3; i++) {
                    ImageView imageView=new ImageView(getContext());
                    Picasso.with(getContext()).load(response.body().getData().get(i).getCover()).into(imageView);
                    mFootImage.add(imageView);
                }
                mFootVPadapter=new DiscoverRecommendFootVPadapter(mFootImage);
                FootViewpager.setAdapter(mFootVPadapter);
                initFootViewIndicator();
                mHandler.sendEmptyMessageDelayed(5,3000);

                    FootViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
//                            if (mData.size() != 0) {
//                                mTabscontainer.getChildAt(position % mData.size()).setEnabled(false);
//                                //  MyLog.i("flag","------position"+position);
//                                mTabscontainer.getChildAt(lastIndex).setEnabled(true);
//                                lastIndex = position % mData.size();
//                                mHandler.sendMessage(Message.obtain(mHandler, 4, position, 0));
//                            }

                            if(mFootImage!=null){
                                FootIndicator.getChildAt(lastIndex).setEnabled(true);
                                FootIndicator.getChildAt(lastIndex).setEnabled(false);
                                lastIndex=position%mFootImage.size();
                                mHandler.sendMessage(Message.obtain(mHandler,5,position,0));
                            }

                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                    });
            }



            @Override
            public void onFailure(Call<DiscoverRecommendFootView> call, Throwable t) {

            }
        });

    }

    private void initFootViewIndicator() {
        for (int i = 0; i < mFootImage.size(); i++) {
            View view=new View(getContext());
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(15,15);
            params.rightMargin=20;
            view.setLayoutParams(params);
            params.gravity = Gravity.CENTER;
            view.setBackgroundResource(R.drawable.recommend_head_select);
            FootIndicator.addView(view);
        }
        FootIndicator.getChildAt(Integer.MAX_VALUE%2).setEnabled(true);

    }


    private void initSpecialData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Urlconfig.RECOMMEND_LISTENER)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRecommendSpecialService service = retrofit.create(DiscoverRecommendSpecialService.class);
        service.special().enqueue(new Callback<DiscoverRecommendDiscoverColumns>() {
            @Override
            public void onResponse(Call<DiscoverRecommendDiscoverColumns> call, Response<DiscoverRecommendDiscoverColumns> response) {
                mList.addAll(response.body().getEditorRecommendAlbums().getList());
                mDiscoveryColumnsBean.addAll(response.body().getDiscoveryColumns().getList());
                mSpecialBean.addAll(response.body().getSpecialColumn().getList());

                // 精品听单
                SpecialTitle1.setText(response.body().getSpecialColumn().getList().get(0).getTitle());
                Specialnum1.setText(response.body().getSpecialColumn().getList().get(0).getFootnote());
                SpecialSubtitle1.setText(response.body().getSpecialColumn().getList().get(0).getSubtitle());
                Picasso.with(getContext()).load(response.body().getSpecialColumn().getList().get(0).getCoverPath()).into(Specialimg1);
                //小编推荐
                EditTitle1.setText(response.body().getEditorRecommendAlbums().getList().get(0).getTrackTitle());
                EditTitle2.setText(response.body().getEditorRecommendAlbums().getList().get(1).getTrackTitle());
                EditTitle3.setText(response.body().getEditorRecommendAlbums().getList().get(2).getTrackTitle());
                Editsubtitle1.setText(response.body().getEditorRecommendAlbums().getList().get(0).getTitle());
                Editsubtitle2.setText(response.body().getEditorRecommendAlbums().getList().get(1).getTitle());
                Editsubtitle3.setText(response.body().getEditorRecommendAlbums().getList().get(2).getTitle());
                Picasso.with(getContext()).load(response.body().getEditorRecommendAlbums().getList().get(0).getCoverSmall()).into(EditImg1);
                Picasso.with(getContext()).load(response.body().getEditorRecommendAlbums().getList().get(1).getCoverSmall()).into(EditImg2);
                Picasso.with(getContext()).load(response.body().getEditorRecommendAlbums().getList().get(2).getCoverSmall()).into(EditImg3);
                // 发现新奇
                Sctext1.setText(response.body().getDiscoveryColumns().getList().get(0).getTitle());
                Sctext2.setText(response.body().getDiscoveryColumns().getList().get(1).getTitle());
                Sctext3.setText(response.body().getDiscoveryColumns().getList().get(2).getTitle());
                Sctext4.setText(response.body().getDiscoveryColumns().getList().get(3).getTitle());
                Sctext5.setText(response.body().getDiscoveryColumns().getList().get(4).getTitle());
                Sctext6.setText(response.body().getDiscoveryColumns().getList().get(5).getTitle());
                Sctext7.setText(response.body().getDiscoveryColumns().getList().get(6).getTitle());
//                Sctext8.setText(response.body().getDiscoveryColumns().getList().get(7).getTitle());
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(0).getCoverPath()).into(Img1);
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(1).getCoverPath()).into(Img2);
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(2).getCoverPath()).into(Img3);
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(3).getCoverPath()).into(Img4);
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(4).getCoverPath()).into(Img5);
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(5).getCoverPath()).into(Img6);
                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(6).getCoverPath()).into(Img7);
//                Picasso.with(getContext()).load(response.body().getDiscoveryColumns().getList().get(7).getCoverPath()).into(Img8);
                //尾布局轮播
//                mList.addAll(response.body().getFocusImages().getList());
//                Message message = mHandler.obtainMessage();
//                message.what = 1;
//                message.obj = mList;
//                mHandler.sendMessage(message);
//                mVPadapter.notifyDataSetChanged();


            }

            @Override
            public void onFailure(Call<DiscoverRecommendDiscoverColumns> call, Throwable t) {

            }
        });

    }


    private void initHotRecommendRetrofit() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.RECOMMEND_HEALTH)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRecommendHotRecommendService service = retrofit.create(DiscoverRecommendHotRecommendService.class);
        service.beans().enqueue(this);

    }


    //TODO
    @Override
    public void onResponse(Call<DiscoverRecommendHotRecommend> call, retrofit2.Response<DiscoverRecommendHotRecommend> response) {
        //热门推荐
        MyLog.i("falg", "------onResponse" + response.body().toString());
        mListBeanData.addAll(response.body().getHotRecommends().getList());

        mGuessList.addAll(response.body().getGuess().getList());

        MyLog.i("flag", "------response" + response.body().getHotRecommends().getList().toString());
        mLVadapter.notifyDataSetChanged();
        //猜你喜欢
        GuessTitle1.setText(response.body().getGuess().getList().get(0).getTitle());
        GuessTitle2.setText(response.body().getGuess().getList().get(1).getTitle());
        GuessTitle3.setText(response.body().getGuess().getList().get(2).getTitle());
        GuessTitle4.setText(response.body().getGuess().getList().get(3).getTitle());
        GuessTitle5.setText(response.body().getGuess().getList().get(4).getTitle());
        GuessTitle6.setText(response.body().getGuess().getList().get(5).getTitle());
        Guesssubtitle1.setText(response.body().getGuess().getList().get(0).getTags());
        Guesssubtitle2.setText(response.body().getGuess().getList().get(1).getTags());
        Guesssubtitle3.setText(response.body().getGuess().getList().get(2).getTags());
        Guesssubtitle4.setText(response.body().getGuess().getList().get(3).getTags());
        Guesssubtitle5.setText(response.body().getGuess().getList().get(4).getTags());
        Guesssubtitle6.setText(response.body().getGuess().getList().get(5).getTags());
        Picasso.with(getContext()).load(response.body().getGuess().getList().get(0).getCoverSmall()).into(Guessimg1);
        Picasso.with(getContext()).load(response.body().getGuess().getList().get(1).getCoverSmall()).into(Guessimg2);
        Picasso.with(getContext()).load(response.body().getGuess().getList().get(2).getCoverSmall()).into(Guessimg3);
        Picasso.with(getContext()).load(response.body().getGuess().getList().get(3).getCoverSmall()).into(Guessimg4);
        Picasso.with(getContext()).load(response.body().getGuess().getList().get(4).getCoverSmall()).into(Guessimg5);
        Picasso.with(getContext()).load(response.body().getGuess().getList().get(5).getCoverSmall()).into(Guessimg6);



    }

    @Override
    public void onFailure(Call<DiscoverRecommendHotRecommend> call, Throwable t) {

    }



    @Override
    public boolean handleMessage(Message message) {
        if (mHandler.hasMessages(2)) {
            mHandler.removeMessages(2);
        }
        switch (message.what) {
            case 1:
                // 在这里获取到数据，并解析
                String result = (String) message.obj;
                //Json解析
                Discover_RecommendBanner discoverRecommendBanner = Discover_RecommendBanner.parseString(result);
                initViewpager(discoverRecommendBanner.getFocusImages().getList(),mData);
                initIndicator(discoverRecommendBanner.getFocusImages().getList(),mData);

//                initViewpager(mList,mData);
//                initIndicator(mList,mData);

                break;
            case 2:
                currentPosition++;
                //   MyLog.i("flag","------"+currentPosition);
                mViewPager.setCurrentItem(currentPosition);
                //这句代码，加不加都没错
                mHandler.sendEmptyMessageDelayed(2, 3000);
                break;
            case 3:
                //这个是拖动viewpager停止，用户拖动的时候延迟1秒轮播。
                mHandler.sendEmptyMessageDelayed(2, 3000);
                break;
            case 4:
                currentPosition = message.arg1;
                mHandler.sendEmptyMessageDelayed(2, 2000);
                break;
            case 5:
//                int pos=FootViewpager.getCurrentItem();
//                if(pos==4){
//                    pos=-1;
//                }
//                FootViewpager.setCurrentItem(pos+1,false);
//                mHandler.sendEmptyMessageDelayed(5,3000);
                currentPosition++;
                //   MyLog.i("flag","------"+currentPosition);
                FootViewpager.setCurrentItem(currentPosition);
                //这句代码，加不加都没错
                mHandler.sendEmptyMessageDelayed(5, 3000);

                break;
        }
        return false;
    }

    int lastindex=0;


    private void initViewpager(List<Discover_Recommend_FocusImageBean> list, List<ImageView> data) {
        //   private void initViewpager(List<DiscoverRecommendDiscoverColumns.FocusImagesBean.ListBean> list, List<ImageView> data) {
        if (list.size() != 0) {
            for (int i = 0; i < list.size(); i++) {
                ImageView imageView = new ImageView(getContext());
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                String path = list.get(i).getPic();

                Picasso.with(getContext()).load(path).into(imageView);
                data.add(imageView);
            }
        }
        mVPadapter.notifyDataSetChanged();
        mHandler.sendEmptyMessageAtTime(2, 3000);
    }

    private void initIndicator(List<Discover_Recommend_FocusImageBean> list, List<ImageView> data) {
        //  private void initIndicator(List<DiscoverRecommendDiscoverColumns.FocusImagesBean.ListBean> list, List<ImageView> data) {
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                View indocator = new View(getContext());
                indocator.setBackgroundResource(R.drawable.recommend_head_select);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
                params.leftMargin = 20;
                params.gravity = Gravity.CENTER;
                indocator.setLayoutParams(params);
                mTabscontainer.addView(indocator);
            }
        }
        //设置单双数显示不同的小球颜色。
        //
        mTabscontainer.getChildAt(Integer.MAX_VALUE % 2).setEnabled(true);
        return ;
    }

    // viewpager onpagerchangelistener
    //记录小球状态
    private int lastIndex = Integer.MAX_VALUE % 2;


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //这里有一点问题，点击回来的时候党页的数据会没有
        //   mData.addAll(mData);
        if (mData.size() != 0) {
            mTabscontainer.getChildAt(position % mData.size()).setEnabled(false);
            //  MyLog.i("flag","------position"+position);
            mTabscontainer.getChildAt(lastIndex).setEnabled(true);
            lastIndex = position % mData.size();
            mHandler.sendMessage(Message.obtain(mHandler, 4, position, 0));

        }

    }

    //当拖动时，让轮播停止
    @Override
    public void onPageScrollStateChanged(int state) {
        switch (state) {
            case ViewPager.SCROLL_STATE_DRAGGING:
                mHandler.sendEmptyMessage(3);
                break;
        }
    }

}
